package com.kelvin.paopao.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.kelvin.kelvinlib.db.dao.DaoSession;
import com.kelvin.kelvinlib.db.dao.UserInfoDao;
import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.responseBean.KefuModel;
import com.kelvin.paopao.api.responseBean.MultipleItem;

import java.util.Date;
import java.util.List;


/**
 * Created by MCK on 2017/5/9.
 */
public class MessageMultiAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {

    private Context context;
    private UserInfoDao dao;

    public MessageMultiAdapter(Context context, DaoSession daoSession, List data) {
        super(data);
        this.context = context;
        dao = daoSession.getUserInfoDao();
        addItemType(MultipleItem.KEFU, R.layout.recycler_message_kefu_item);
        addItemType(MultipleItem.COMMON, R.layout.recycler_message_item);
        addItemType(MultipleItem.KEFU_COLLECTION, R.layout.recycler_message_kefu_collection_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultipleItem multipleItem) {
        switch (helper.getItemViewType()) {
            case MultipleItem.KEFU:
                KefuModel kefuModel = (KefuModel) multipleItem.getData();
                helper.setText(R.id.tv_name, kefuModel.getUser_name() + "(客服)");
                ImageView ivTouxiang = helper.getView(R.id.iv_touxiang);
                GlideUtil.loadImgCircle(kefuModel.getUser_headimg(), ivTouxiang, context, R.drawable.default_img);
                break;
            case MultipleItem.KEFU_COLLECTION:
                break;
            case MultipleItem.COMMON:
                EMConversation item = (EMConversation) multipleItem.getData();
                TextView tvName = helper.getView(R.id.tv_name);
                TextView tv_detail = helper.getView(R.id.tv_detail);
                tv_detail.setVisibility(View.INVISIBLE);

                tvName.setText(EaseSmileUtils.getSmiledText(context, EaseCommonUtils.
                        getMessageDigest(item.getLastMessage()
                                , (this.context))), TextView.BufferType.SPANNABLE);
                ImageView iv = helper.getView(R.id.iv_touxiang);
                String user_headimg = dao.load(Long.parseLong(
                        item.conversationId()))
                        .getAvatar();
                GlideUtil.loadImgCircle(user_headimg, iv, context, R.drawable.default_img);
                Date lastMsgDate = new Date(item.getLastMessage().getMsgTime());
                String lastMsgTime = CommonUtil.parseDateTime(lastMsgDate, "MM月dd日");
                helper.setText(R.id.tv_date, lastMsgTime);

                TextView tvUnreadCount = helper.getView(R.id.unread_msg_number);
                if (item.getUnreadMsgCount() != 0) {
                    tvUnreadCount.setVisibility(View.VISIBLE);
                    tvUnreadCount.setText(item.getUnreadMsgCount() + "");
                } else {
                    tvUnreadCount.setVisibility(View.GONE);
                }
                RelativeLayout rlDelelte = helper.getView(R.id.rl_delete);
                if (showDelete) {
                    rlDelelte.setVisibility(View.VISIBLE);
                } else {
                    rlDelelte.setVisibility(View.GONE);
                }
                helper.addOnClickListener(R.id.rl_delete);
        }
    }

    private boolean showDelete;

    public void setDelete(boolean showDelete) {
        this.showDelete = showDelete;
        notifyDataSetChanged();
    }
}
